<?php

class Admin_ProductTypeController extends JControllerAdmin
{
    /**
     * @return void
     */
    public function init()
    {
        $layout = Zend_Layout::getMvcInstance();
        $actionName = $this->_request->getActionName();
        switch ($actionName) {
            case 'add':
            case 'update':
                $layout->setLayout('admin/layout-admin-add');
                break;
        }
    }

    /**
     * @return void
     */
    public function indexAction()
    {
        /** @var Zend_Controller_Action_Helper_FlashMessenger $flashMessenger */
        $flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $messages = $flashMessenger->getMessages();
        $this->view->messages = $messages;

        // BEGIN: Tạo đối tượng Paginator
        $dbSelect = JDbAdapter::factory()
            ->select()
            ->from(
                array('pt' => 'PRODUCT_TYPES'),
                array('pt.*', 'NumberOfProducts' => 'COUNT(p.ID)'))
            ->joinLeft(
                array('p' => 'PRODUCTS'),
                'pt.ID = p.ProductTypeID',
                null
            )
            ->where('pt.Deleted = ?', false)
            ->group('pt.ID');

        // Tìm kiếm theo tên sản phẩm (nếu có)
        $keyWords = $this->_request->getParam('keyWords');
        if (isset($keyWords)) {
            $dbSelect->where("pt.Name LIKE '%$keyWords%'");
            $this->view->enteredKeyWords = $keyWords;
        }

        $paginator = new Zend_Paginator(
            new Zend_Paginator_Adapter_DbSelect($dbSelect)
        );
        // END: Tạo đối tượng Paginator

        // Get and set current page
        $page = $this->_request->getParam('page');
        $paginator->setCurrentPageNumber($page);
        $productTypes = $paginator->getItemsByPage($page);

        // Assign data for view
        $this->view->productTypes = $productTypes;
        $this->view->paginator = $paginator;
    }

    /**
     * @return void
     */
    public function addAction()
    {
        $this->view->messages = array();

        $isPost = $this->_request->getParam('isPost');
        if (isset($isPost)) {
            $name = $this->_request->getParam('name');
            $description = $this->_request->getParam('description');

            $newProductTypeID = Application_Model_ProductType::addProductType(
                $name, $description
            );
            if ($newProductTypeID == 0) {
                $this->view->messages = array('Add Product Type Fail');
                $this->view->name = $name;
                $this->view->description = $description;
            } else {
                /** @var Zend_Controller_Action_Helper_FlashMessenger $flashMessenger */
                $flashMessenger = $this->_helper->getHelper('FlashMessenger');
                $task = $this->_request->getParam('task');
                switch ($task) {
                    case 'apply':
                        $flashMessenger->addMessage('Add Product Type Successfully');
                        $this->redirect(
                            "/admin/productType/update/productTypeID/$newProductTypeID"
                        );
                        break;
                    case 'apply-and-close':
                        $flashMessenger->addMessage('Add Product Type Successfully');
                        $this->redirect('/admin/productType/index');;
                        break;
                    case 'apply-and-new':
                        $this->view->messages = array('Add Product Type Successfully');
                        break;
                    default:
                        $flashMessenger->addMessage('Cannot Determine Next Task');
                        $this->redirect('/admin/productType/index');
                        break;
                }
            }
        }
    }

    /**
     * @return void
     */
    public function deleteAction()
    {
        /** @var Zend_Controller_Action_Helper_FlashMessenger $flashMessenger */
        $flashMessenger = $this->_helper->getHelper('FlashMessenger');

        $isPost = $this->_request->getParam('isPost');
        if (!isset($isPost)) {
            $flashMessenger->addMessage('Cannot Execute This URL Directly');
            $this->redirect('/admin/product/index');
        }

        $productTypeIDs = $this->_request->getParam('productTypeIDs');
        if (!Application_Model_ProductType::deleteProductTypes($productTypeIDs)) {
            $flashMessenger->addMessage('Delete Fail');
        } else {
            $var = '';

            $length = count($productTypeIDs);
            for ($i = 0; $i < $length - 1; $i++) {
                $var .= $productTypeIDs[$i] . ', ';
            }
            $var .= $productTypeIDs[$length - 1];

            $flashMessenger->addMessage("Delete $var Successfully");
        }

        $this->redirect($_SERVER['HTTP_REFERER']);
    }

    /**
     * @return void
     */
    public function updateAction()
    {
        /** @var Zend_Controller_Action_Helper_FlashMessenger $flashMessenger */
        $flashMessenger = $this->_helper->getHelper('FlashMessenger');
        $messages = $flashMessenger->getMessages();
        $this->view->messages = $messages;

        $isPost = $this->_request->getParam('isPost');
        if (isset($isPost)) {
            $id = $this->_request->getParam('productTypeID');
            $name = $this->_request->getParam('name');
            $description = $this->_request->getParam('description');

            $success = Application_Model_ProductType::updateProductType(
                $id, $name, $description
            );
            if (!$success) {
                $this->view->messages = array('Update Product Type Fail');;
                $this->view->name = $name;
                $this->view->description = $description;
            } else {
                $task = $this->_request->getParam('task');
                switch ($task) {
                    case 'apply':
                        $this->view->messages = array('Update Product Type Successfully');
                        $this->view->name = $name;
                        $this->view->description = $description;
                        break;
                    case 'apply-and-close':
                        $flashMessenger->addMessage('Update Product Type Successfully');
                        $this->redirect('/admin/productType/index');;
                        break;
                    default:
                        $flashMessenger->addMessage('Cannot Determine Next Task');
                        $this->redirect('/admin/productType/index');
                        break;
                }
            }
        } else {
            $productTypeID = $this->_request->getParam('productTypeID');
            $productTypePOJO = Application_Model_ProductType::getProductType($productTypeID);
            if ($productTypePOJO == null) {
                $flashMessenger->addMessage('This ID does not exist');
                $this->redirect('/admin/productType/index');
            }
            $this->view->productTypePOJO = $productTypePOJO;
        }

        $this->render('add');
    }
}
